package com.qiguliuxing.dts.service.base;

import com.google.common.collect.Lists;
import com.qiguliuxing.dts.common.constant.CommConsts;
import com.qiguliuxing.dts.db.dao.DtsMerchantWithdrawalAccountMapper;
import com.qiguliuxing.dts.db.domain.DtsMerchantWithdrawalAccount;
import com.qiguliuxing.dts.db.domain.DtsMerchantWithdrawalAccountExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liumeng
 */
@Service
public class DtsMerchantWithdrawalService {

    @Resource
    private DtsMerchantWithdrawalAccountMapper merchantWithdrawalAccountMapper;

    public List<DtsMerchantWithdrawalAccount> findByBrandId(Integer brandId) {
        DtsMerchantWithdrawalAccountExample example = new DtsMerchantWithdrawalAccountExample();
        example.setOrderByClause("id asc");
        DtsMerchantWithdrawalAccountExample.Criteria criteria = example.createCriteria();
        criteria.andBrandIdEqualTo(brandId);
        List<DtsMerchantWithdrawalAccount> merchantWithdrawalAccountList = merchantWithdrawalAccountMapper.selectByExample(example);
        if (!CollectionUtils.isNotEmpty(merchantWithdrawalAccountList) || merchantWithdrawalAccountList.size() < CommConsts.ACCOUNT_COUNT) {
            int size = CollectionUtils.isEmpty(merchantWithdrawalAccountList) ? 0 : merchantWithdrawalAccountList.size();
            merchantWithdrawalAccountList.addAll(this.addNewAccountBean(CommConsts.ACCOUNT_COUNT - size));
        }
        return merchantWithdrawalAccountList;
    }

    private List<DtsMerchantWithdrawalAccount> addNewAccountBean(int size){
        List<DtsMerchantWithdrawalAccount> result = Lists.newArrayList();
        for (int i = 0; i < size; i++) {
            result.add(new DtsMerchantWithdrawalAccount());
        }
        return result;
    }
}
